// pages/goods/goods.js
const app = getApp()
var host = app.globalData.host;
Page({
    data: {
        currentTab: 0,
        books: [],
        host: host

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(e) {

        console.log(e) //打印出一开始带过来的东西
        var type = e.id;
        this.setData({ currentTab: type });
        this.getBookList(type);

        console.log(type) //用于测试




    },



    getBookList: function(type) {
        console.log(type)
        var page = this;
        var result = []
        wx.request({
            url: host + '/api/goods/getGoodsListByType',
            data: { type: type },
            header: { 'Content-Type': 'application/json' },
            method: 'GET',
            success: function(res) {
                console.log(res)
                var books = res.data.data;
                for (var i = 0; i < books.length; i++) {
                    var book = books[i];
                    result.push(book)
                        // console.log(result)
                }
                page.setData({ books: result });
            },

        });
    }, // 实现切换导航栏
    switchNav: function(e) {
        console.log(e)
        var page = this;
        var type = e.target.dataset.current;
        if (this.data.currentTab == type) {
            return false;
        } else {
            page.setData({ currentTab: type });
            this.getBookList(type);
        }

    },
})